import threading
import time      
       
class Broker(threading.Thread):    
    def __init__(self,callbacks,frameBuffer):
        '''
        Broker used to dispatch data to other modules
            usage:  myBroker = Broker(callbacks,FrameBuffer)
        '''
        threading.Thread.__init__(self)
        self.__running = False
        self.__callbacks = callbacks
        self.__buffer = frameBuffer
        self.__done = 0
        
    def isRunning(self):
        '''Returns True if the capture is running, else return False.'''
        return self.__running    
        
    def stop(self):
        '''Stops the thread.'''
        self.__running = False
        
    def run(self):
        '''Thread main loop'''
        self.__running = True
        while self.isRunning():
            if self.__buffer.count() > 0:
                pktlen,pkt,ts = self.__buffer.getpkt()
                self.__done += 1
                for callback in self.__callbacks:
                    callback(pktlen,pkt,ts)
            else:
                time.sleep(0.01)
                    